﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Carbon.Web.Forms.Controls
{
    public class CollectionEditorItem : Control, IDataItemContainer, INamingContainer, IDataProxy
    {
        public CollectionEditorItem(int itemIndex, ListItemType itemType)
        {
            this.m_DataItemIndex = itemIndex;
            this.m_ItemType = itemType;
        }

        public CollectionEditorItem(int itemIndex, ListItemType itemType, object data)
        {
            this.m_DataItemIndex = itemIndex;
            this.m_ItemType = itemType;
            this.m_DataItem = data;
        }

        private Object m_DataItem = default(Object);
        public virtual Object DataItem
        {
            get
            {
                return m_DataItem;
            }
        }
        private int m_DataItemIndex = default(int);
        public virtual int DataItemIndex
        {
            get
            {
                return m_DataItemIndex;
            }
            internal set
            {
                m_DataItemIndex = value;
            }
        }

        public virtual int DisplayIndex
        {
            get
            {
                return DataItemIndex;
            }
        }

        public bool IsItem
        {
            get
            {
                return ItemType == ListItemType.Item || ItemType == ListItemType.AlternatingItem;
            }
        }

        private ListItemType m_ItemType = default(ListItemType);
        public ListItemType ItemType
        {
            get
            {
                return m_ItemType;
            }
            internal set
            {
                m_ItemType = value;
            }
        }

        object IDataProxy.DataContext
        {
            get 
            { 
                return DataItem; 
            }
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            if (args is CommandEventArgs)
            {
                args = new CollectionEditorCommandEventArgs((CommandEventArgs)args, source, this);
                source = this;

                RaiseBubbleEvent(this, args);

                return true;
            }
            else
            {
                return base.OnBubbleEvent(source, args);
            }
        }
    }
}
